FormUtil = {
	newForm : function() {
		var form = App.FormVo.create({id: null, headline: "headline", // uid: Math.uuid(), 
			oncePerIp: true, displayQrCode: "", status: 1, updateTime: "",
			showButton: true,
			fields: []
		});
		return form;
	},
	editFormSettings : function(formId) {
		$("#tabbar_panel li").removeClass("active");
		$("#tabbar_panel li:eq(2)").addClass("active");

		var child = App.FormSettingsView.create();
		var containerView = Em.View.views['nav_tab_view'];
		containerView.get('childViews').forEach(function(item) {
			item.remove();
		});
		containerView.removeAllChildren();
		containerView.get('childViews').pushObject(child);
		child.set("context", Vari.frm);
	},
	retriveTpl: function() {
		var rt = null;				
		$.ajax({
			type : 'GET',
			dataType : 'json', contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'fields',

			success : function(json) {
				rt = FieldUtil.genFiledVos(json.flds);								
			}
		});
		return rt;
	},
	
	retriveList: function() {
		var rt = [];
		$.ajax({
			type : 'GET',
			dataType : 'json', 
			//contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'forms',
			data: {appId: Vari.CurrAppId},
			success : function(json) {
				Vari.CurrAppId = json.appId;
				Vari.UserEmail = json.email;
				var i = 1;
				json.frms.forEach(function(formTo) {
					var formVo = FormUtil.genFormVo(formTo, i++);					
					rt.pushObject(formVo);
				});
			}
		});
		return rt;
	},
	
	retriveInfo: function(formId) {
		if (formId == null) {
			return Vari.frm;
		}
		
		var rt = null;				
		$.ajax({
			type : 'GET',
			dataType : 'json', contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'forms/' + formId,

			success : function(json) {
				Vari.UserEmail = json.email;
				rt = FormUtil.genFormVo(json.frm);								
			}
		});				
		return rt;
	},
	
	retriveData: function(formId) {
		var rt = null;
		$.ajax({
			type : 'GET',
			dataType : 'json', contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'form/data/' + formId,
			
			success : function(json) {
				Vari.UserEmail = json.email;
				rt = json;
			}
		});
		return rt;
	},
	
	retriveReport: function(formId) {
		var rt = null;
		$.ajax({
			type : 'GET',
			dataType : 'json', contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'form/report/' + formId,
			
			success : function(json) {
				Vari.UserEmail = json.email;
				rt = json;
			}
		});
		return rt;
	},
	genFormVo: function (formTo, i){		
		var formVo = App.FormVo.create();
		
		Ember.mixin(formVo, {
			id: formTo.id,
			headline: formTo.headline,
			oncePerIp: formTo.oncePerIp,
			showButton: formTo.showButton,
			status: formTo.status,
			apiVer: formTo.apiVer,
			displayQrCode: formTo.displayQrCode,
			userId: formTo.userId,
			appId: formTo.appId,
			updateTime: $.dformat.date(formTo.updateTime, "dd/MM/yyyy"),
			num: i
		});
		var fieldVos = [];
		formTo.fields.forEach(function(fieldTo) {
			var fieldVo = FieldUtil.genFieldVo(fieldTo);			
			fieldVos.pushObject(fieldVo);
			Vari.fieldMap.set(fieldVo.get("uid"), fieldVo);
		});
		
		Ember.mixin(formVo, {
			fields: fieldVos,
		});
		return formVo;
	},
};